﻿// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This file is part of CodingEditor.
// Note:	This project is derived from Peter Project
//			(hosted on sourceforge and codeplex)
//
// Copyright (c) 2008-2009, CE Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


namespace CE.CustomControls
{
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class CustomRenderer : ToolStripProfessionalRenderer
    {
        #region Fields

        private Color _BgEndColor = Color.FromArgb(244, 247, 252);
        private Color _BgStartColor = Color.FromArgb(233, 236, 250);
        private Color _ImgMarginColor1 = Color.FromArgb(243, 246, 251);
        private Color _ImgMarginColor2 = Color.FromArgb(213, 218, 232);
        private Color _ItemColor1 = Color.FromArgb(239, 247, 250);
        private Color _ItemColor2 = Color.FromArgb(186, 228, 246);
        private Color _ItemOutlineColor = Color.FromArgb(150, 217, 249);

        #endregion Fields

        #region Constructors

        public CustomRenderer()
            : base()
        {
        }

        #endregion Constructors

        #region Methods

        protected override void OnRenderButtonBackground(ToolStripItemRenderEventArgs e)
        {
            RenderButtonBackground(e);
        }

        protected override void OnRenderDropDownButtonBackground(ToolStripItemRenderEventArgs e)
        {
            RenderButtonBackground(e);
        }

        protected override void OnRenderImageMargin(ToolStripRenderEventArgs e)
        {
            using (LinearGradientBrush b = new LinearGradientBrush(e.AffectedBounds,
                                                                   _ImgMarginColor1, _ImgMarginColor2, LinearGradientMode.Horizontal)) {
                e.Graphics.FillRectangle(b, e.AffectedBounds);
            }
            base.OnRenderImageMargin(e);
        }

        protected override void OnRenderItemBackground(ToolStripItemRenderEventArgs e)
        {
            base.OnRenderItemBackground(e);
        }

        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            e.TextColor = Color.Black;
            base.OnRenderItemText(e);
        }

        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            Graphics g = e.Graphics;
            if ((e.Item.Selected || e.Item.Pressed) && e.Item.Enabled) {
                if (e.Item.OwnerItem == null && e.ToolStrip.GetType().Name == "MenuStrip") {
                    RectangleF rect = new RectangleF(0, 0, e.Item.Bounds.Width, e.Item.Bounds.Height - 2);
                    _ItemOutlineColor = Color.FromArgb(152, 191, 216);
                    using (LinearGradientBrush b = new LinearGradientBrush(rect, _ItemColor1, _ItemColor2,
                        LinearGradientMode.Vertical)) {
                        g.FillRectangle(b, rect);
                        PointF[] points = new PointF[5];
                        points[0] = new PointF(rect.X + 1, rect.Y + 1);
                        points[1] = new PointF(rect.Right - 1, rect.Y + 1);
                        points[2] = new PointF(rect.Right - 1, rect.Bottom - 1);
                        points[3] = new PointF(rect.X + 1, rect.Bottom - 1);
                        points[4] = new PointF(rect.X + 1, rect.Y + 1);
                        g.DrawLines(new Pen(_ItemOutlineColor, 1.0f), points);
                    }
                } else {
                    _ItemOutlineColor = Color.FromArgb(150, 217, 249);
                    using (LinearGradientBrush b = new LinearGradientBrush(new Rectangle(0, 0,
                           e.Item.Bounds.Width, e.Item.Bounds.Height), _ItemColor1, _ItemColor2, 90)) {
                        using (GraphicsPath path = GetRoundedRectPath(
                            new Rectangle(1, 1, e.Item.Bounds.Width - 1, e.Item.Bounds.Height - 1), 3)) {
                            g.FillPath(b, path);
                            g.DrawPath(new Pen(_ItemOutlineColor, 1.0f), path);
                        }
                    }
                }
            }

            base.OnRenderItemBackground(e);
        }

        protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)
        {
            e.Item.ForeColor = Color.Black;
            base.OnRenderSeparator(e);
        }

        protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e)
        {
            LinearGradientBrush b = new LinearGradientBrush(e.AffectedBounds, _BgStartColor, _BgEndColor, LinearGradientMode.Horizontal);
            e.Graphics.FillRectangle(b, e.AffectedBounds);
            b.Dispose();
            base.OnRenderToolStripBackground(e);
        }

        protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
        {
            base.OnRenderToolStripBorder(e);
        }

        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = 2 * radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // top left
            path.AddArc(arcRect, 190, 90);

            // top right
            arcRect.X = rect.Right - diameter - 1;
            path.AddArc(arcRect, 280, 90);

            // bottom right
            arcRect.Y = rect.Bottom - diameter - 1;
            path.AddArc(arcRect, 10, 90);

            // bottom left
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 100, 90);
            path.CloseFigure();
            return path;
        }

        private void RenderButtonBackground(ToolStripItemRenderEventArgs e)
        {
            _ItemOutlineColor = Color.FromArgb(150, 217, 249);
            if ((e.Item.Pressed || e.Item.Selected) && e.Item.Enabled) {
                using (LinearGradientBrush b = new LinearGradientBrush(
                               new Rectangle(0, 0, e.Item.Bounds.Width, e.Item.Bounds.Height), _ItemColor1,
                               _ItemColor2, LinearGradientMode.Vertical)) {
                    using (GraphicsPath path = GetRoundedRectPath(new Rectangle(0, 0, e.Item.Bounds.Width,
                                                                                e.Item.Bounds.Height), 3)) {
                        e.Graphics.FillPath(b, path);
                        e.Graphics.DrawPath(new Pen(_ItemOutlineColor, 1.0f), path);
                    }
                }
            }
        }

        #endregion Methods
    }
}